"""
获取最新的K线数据并添加到处理器
"""
import pandas as pd
from typing import List
from src.okx.models import KlineData


def convert_klines_to_dataframe(klines: List[KlineData]) -> pd.DataFrame:
    """将KlineData列表转换为DataFrame"""
    if not klines:
        return pd.DataFrame()

    data = {
        'timestamp': [k.timestamp for k in klines],
        'open': [k.open for k in klines],
        'high': [k.high for k in klines],
        'low': [k.low for k in klines],
        'close': [k.close for k in klines],
        'volume': [k.volume for k in klines],
        'quote_volume': [k.quote_volume for k in klines],
    }

    df = pd.DataFrame(data)
    df['datetime'] = pd.to_datetime(df['timestamp'], unit='ms')
    df = df.sort_values('timestamp').reset_index(drop=True)

    return df
